home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / thrust-s.53 / thrust-s / thrust / src / highscore.c < prev    next >
C/C++ Source or Header  |  1995-10-12  |  2KB  |  100 lines

  1.  
  2. /* Written by Peter Ekberg, peda@lysator.liu.se */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include "thrust.h"
  9.  
  10. highscoreentry highscorelist[HIGHSCORES];
  11.  
  12. void
  13. writehighscores(void)
  14. {
  15.   FILE *fp;
  16.  
  17.   fp=fopen(HIGHSCOREFILE, "wb");
  18.   if(fp==NULL)
  19.     return;
  20.   fwrite(highscorelist, sizeof(highscoreentry), HIGHSCORES, fp);
  21.   fclose(fp);
  22. }
  23.  
  24. int
  25. readhighscores(void)
  26. {
  27.   FILE *fp;
  28.   int res;
  29.  
  30.   fp=fopen(HIGHSCOREFILE, "rb");
  31.   if(fp==NULL)
  32.     return(0);
  33.   res = fread(highscorelist, sizeof(highscoreentry), HIGHSCORES, fp);
  34.   if(res != HIGHSCORES)
  35.     return(0);
  36.   fclose(fp);
  37.  
  38.   return(1);
  39. }
  40.  
  41. char *
  42. standardname(void)
  43. {
  44.   char *tmp;
  45.   static char name[40];
  46.   int i;
  47.  
  48.   tmp=getenv("USER");
  49.   if(tmp==NULL)
  50.     tmp=getenv("LOGNAME");
  51.   if(tmp==NULL)
  52.     name[0]=0;
  53.   else {
  54.     strncpy(name, tmp, 39);
  55.     name[39]=0;
  56.     for(i=0; i<strlen(name); i++)
  57.       name[i]=toupper(name[i]);
  58.   }
  59.  
  60.   return(name);
  61. }
  62.  
  63. int
  64. inithighscorelist(void)
  65. {
  66.   int i;
  67.   
  68.   if(!readhighscores()) {
  69.     for(i=0; i<HIGHSCORES; i++) {
  70.       strcpy(highscorelist[i].name, "JOHN DOE");
  71.       highscorelist[i].score=(5-i)*1000;
  72.     }
  73.   }
  74.   
  75.   return(0);
  76. }
  77.  
  78. int
  79. ahighscore(int score)
  80. {
  81.   return(score>highscorelist[HIGHSCORES-1].score);
  82. }
  83.  
  84. void
  85. inserthighscore(char *name, int score)
  86. {
  87.   int i;
  88.  
  89.   for(i=HIGHSCORES; i>0 && score>highscorelist[i-1].score; i--) {
  90.     if(i<HIGHSCORES) {
  91.       strcpy(highscorelist[i].name, highscorelist[i-1].name);
  92.       highscorelist[i].score = highscorelist[i-1].score;
  93.     }
  94.   }
  95.   if(i<HIGHSCORES) {
  96.     strcpy(highscorelist[i].name, name);
  97.     highscorelist[i].score = score;
  98.   }
  99. }
  100.